﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;

namespace University.Workshop.WinApp
{
    public partial class UserRegistration : Form
    {
        public UserRegistration()
        {
            InitializeComponent();
        }

        private void btnRegistrar_Click(object sender, EventArgs e)
        {
            try
            {
                UserManager.RegisterNewUser(
                    txtId.Text,
                    txtFirstName.Text,
                    txtLastName.Text,
                    txtTelephone.Text,
                    txtCellPhone.Text,
                    txtAddress.Text,
                    txtType.Text,
                    txtLogin.Text,
                    txtPassword.Text,
                    txtRePassword.Text
                );
                MessageBox.Show("Usuario registrado exitosamente");
            }
            catch (Exception ex)
            {
                labelMessage.Text = ex.Message;

                if (ex.Message == "Todos los campos son requeridos para el registro.")
                        {
                            if (string.IsNullOrEmpty(txtFirstName.Text))
                                txtFirstName.Focus();
                            else
                                if (string.IsNullOrEmpty(txtLastName.Text))
                                    txtLastName.Focus();
                                else
                                    if (string.IsNullOrEmpty(txtId.Text))
                                        txtId.Focus();
                                    else
                                        if (string.IsNullOrEmpty(txtTelephone.Text))
                                            txtTelephone.Focus();
                                        else
                                            if (string.IsNullOrEmpty(txtCellPhone.Text))
                                                txtCellPhone.Focus();
                                            else
                                                if (string.IsNullOrEmpty(txtAddress.Text))
                                                    txtAddress.Focus();
                                                else
                                                    if (string.IsNullOrEmpty(txtType.Text))
                                                        txtType.Focus();
                                                    else
                                                        if (string.IsNullOrEmpty(txtLogin.Text))
                                                            txtLogin.Focus();
                                                        else
                                                            if (string.IsNullOrEmpty(txtPassword.Text))
                                                                txtPassword.Focus();
                                                            else
                                                                if (string.IsNullOrEmpty(txtRePassword.Text))
                                                                    txtRePassword.Focus();
                        }
                else
                    if (ex.Message == "El nombre de usuario no está disponible, ya se encuentra registrado.")
                    {
                        txtLogin.Focus();
                        txtLogin.SelectAll();
                    }
                    else
                        if (ex.Message == "El carnet de identidad ya se encuentra registrado en el sistema.")
                        {
                            txtId.Focus();
                            txtId.SelectAll();
                        }   
                else

                if (ex.Message == "Los passwords introducidos no coinciden. Vuelva a intentarlo."
                    || ex.Message == "Password inválido, debe tener de 8 a 20 carácteres.")
                {
                    txtPassword.Focus();
                    txtPassword.SelectAll();
                }
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
